/**
 *  CSV Parser
 *  Luciano RUBIO <luciano@loociano.com>
 *
 * This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "Item.hpp"

/**
 * Constructor
 */
Item::Item()
{
	this->meal = "";
	this->brand = "";
	this->name = "";
	this->servings = 0;

	this->calories_in = 0;
	this->sugars_in = 0;
	this->carbs_in = 0;
	this->fats_in = 0;
	this->protein_in = 0;
	this->cholesterol_in = 0;
	this->sodium_in = 0;
	this->fiber_in = 0;

	this->calories = 0;
	this->sugars = 0;
	this->carbs = 0;
	this->fats = 0;
	this->protein = 0;
	this->cholesterol = 0;
	this->sodium = 0;
	this->fiber = 0;
}

/**
 * Destructor
 */
Item::~Item()
{

}

double Item::parseString(string s)
{
	s = removeUnits(s);
	s = toLowerCase(s);
	replace(s.begin(), s.end(), ',', '.');
	return stringToDouble(s);
}

/**
 * Remove the units grams or milligrams
 */
string Item::removeUnits(string s)
{
	int length = s.length();

	if (!s.empty())
	{
		if (s.at(length - 1) == kGram)
		{
			if (length > 1)
			{
				if (s.at(length - 2) == kMilli)
				{
					// Remove suffix 'mg'
					return s.substr(0, length - 2);
				}
			}
			// Remove suffix 'g'
			return s.substr(0, length - 1);
		}
	}
	return s;
}

/**
 * Parses string to double, removing units if existing
 */
double Item::stringToDouble(string string)
{
	double x;
	istringstream i(string);

	if (!(i >> x))
		return 0;
	return x;
}

/**
 * Prints data stored for item
 */
void Item::printData()
{
	cout << this->servings << " serv. of " << this->name << "(" << this->brand
			<< ") for " << this->meal << endl;

	cout << "(" << this->calories << " calories, " << this->sugars
			<< "g sugars, " << this->carbs << "g carbs, " << this->fats
			<< "g fat, " << this->protein << "g protein, " << this->cholesterol
			<< "mg cholesterol, " << this->sodium << "mg sodium, "
			<< this->fiber << "g fiber)" << endl;

	cout << "(" << this->calories_in << " calories, " << this->sugars_in
			<< "g sugars, " << this->carbs_in << "g carbs, " << this->fats_in
			<< "g fat, " << this->protein_in << "g protein, "
			<< this->cholesterol_in << "mg cholesterol, " << this->sodium_in
			<< "mg sodium, " << this->fiber_in << "g fiber)" << endl;
}

const string& Item::getBrand() const
{
	return brand;
}

void Item::setBrand(const string& brand)
{
	this->brand = toLowerCase(brand);
}

double Item::getCalories() const
{
	return calories;
}

void Item::setCalories(double calories)
{
	this->calories = calories;
}

double Item::getCaloriesIn() const
{
	return calories_in;
}

void Item::setCaloriesIn(double caloriesIn)
{
	calories_in = caloriesIn;
}

double Item::getCarbs() const
{
	return carbs;
}

void Item::setCarbs(double carbs)
{
	this->carbs = carbs;
}

double Item::getCarbsIn() const
{
	return carbs_in;
}

void Item::setCarbsIn(double carbsIn)
{
	carbs_in = carbsIn;
}

double Item::getCholesterol() const
{
	return cholesterol;
}

void Item::setCholesterol(double cholesterol)
{
	this->cholesterol = cholesterol;
}

double Item::getCholesterolIn() const
{
	return cholesterol_in;
}

void Item::setCholesterolIn(double cholesterolIn)
{
	cholesterol_in = cholesterolIn;
}

double Item::getFats() const
{
	return fats;
}

void Item::setFats(double fats)
{
	this->fats = fats;
}

double Item::getFatsIn() const
{
	return fats_in;
}

void Item::setFatsIn(double fatsIn)
{
	fats_in = fatsIn;
}

double Item::getFiber() const
{
	return fiber;
}

void Item::setFiber(double fiber)
{
	this->fiber = fiber;
}

double Item::getFiberIn() const
{
	return fiber_in;
}

void Item::setFiberIn(double fiberIn)
{
	fiber_in = fiberIn;
}

const string& Item::getMeal() const
{
	return meal;
}

void Item::setMeal(const string& meal)
{
	this->meal = meal;
}

const string& Item::getName() const
{
	return name;
}

void Item::setName(const string& name)
{
	this->name = toLowerCase(name);
}

double Item::getProtein() const
{
	return protein;
}

void Item::setProtein(double protein)
{
	this->protein = protein;
}

double Item::getProteinIn() const
{
	return protein_in;
}

void Item::setProteinIn(double proteinIn)
{
	protein_in = proteinIn;
}

double Item::getServings() const
{
	return servings;
}

void Item::setServings(double servings)
{
	this->servings = servings;
}

double Item::getSodium() const
{
	return sodium;
}

void Item::setSodium(double sodium)
{
	this->sodium = sodium;
}

double Item::getSodiumIn() const
{
	return sodium_in;
}

void Item::setSodiumIn(double sodiumIn)
{
	sodium_in = sodiumIn;
}

double Item::getSugars() const
{
	return sugars;
}

void Item::setSugars(double sugars)
{
	this->sugars = sugars;
}

double Item::getSugarsIn() const
{
	return sugars_in;
}

void Item::setSugarsIn(double sugarsIn)
{
	sugars_in = sugarsIn;
}
